﻿$(function() {
    createForm();
    getCourseInfo();

    $("#btn_upload").click(function() {        
        $(this).attr("disabled", "disabled");
        $('#uploadify').uploadifyUpload();
    });    
});

function createForm() {
    if (inframe == 0) {
        $(".dataform").before('\
            <div class="functionHeader">\
                <div class="title">课程管理</div>\
            </div>\
            <div class="functionBtns">\
                <div class="btns">\
                    <img src="' + rootpath + 'images/new.gif" alt="new" width="20" height="16" align="top" /><a href="addcourse.aspx" class="active">添加课程</a>\
                    <img src="' + rootpath + 'images/list.gif" alt="list" width="20" height="16" align="top" /><a href="default.aspx">课程列表</a>\
                </div>\
                <div class="help">\
                    <img src="' + rootpath + 'images/return.gif" alt="list" width="20" height="16" align="top" /><a href="default.aspx">返回</a>\
                </div>\
                <div class="clear"></div>\
            </div>\
        ');
        formReady();
    }
    else {
        formloaded();
    }
}

function getCourseInfo() {
    $.ajax({
        type: "get",
        url: rootpath + "api/course.ashx",
        data: { type: "getcourseinfo", courseid: courseId, r: Math.random() },
        dataType: "json",
        async: false,
        success: function(data) {
            if (data.coursetype != 1) {
                facebox("课程不是Scorm1.2标准的");
                window.location.href = 'default.aspx';
                return;
            }
            if (data.status == 1) {
                facebox("审核通过的课程不能修改章节");
                window.location.href = 'default.aspx';
                return;
            }
            $("#uploadify").uploadify({
                'uploader': rootpath + 'swf/uploadify.swf',
                'script': 'create.ashx',
                'scriptData': { type: 'uploadscorm', cid: courseId, serialnumber: data.serialnumber, schoolid: data.schoolid },
                'cancelImg': rootpath + 'images/cancel.png',
                'buttonImg': rootpath + 'images/browse-files.png',
                'folder': rootpath + 'data/course/' + data.schoolid + '/' + data.serialnumber,
                'queueID': 'fileQueue',
                'displayData': 'speed',
                'auto': false,
                'multi': false,
                'width': 130,
                'sizeLimit': 1024 * 1024 * 600,
                onSelect: function() {
                    $("#fileQueue").show();
                    $("#btn_upload").attr("disabled", "");
                    formloaded();
                },
                onOpen: function() {
                    $("#fileQueue").dialog("open");
                },
                onComplete: function(event, queueid, fileobj) {
                    $("#btn_upload").attr("disabled", "disabled");
                    $("#fileQueue").hide();
                    importSection(fileobj.filePath, data.serialnumber);

                },
                onError: function() {
                    $("#btn_upload").attr("disabled", "");
                }
            });
        }
    });
}
function importSection(filePath, serialnumber) {
    facebox("正在导入章节...");
    $.ajax({
        type: "get",
        url: "create.ashx",
        data: { type: "importsection", cid: courseId, filePath: filePath, r: Math.random() },
        dataType: "string",
        success: function(data) {
            facebox("课程上传成功！");
            if (inframe == 0) {
                window.location.href = "preview.aspx?cid=" + courseId;
            }
            else {
                closeDialog();
            }           
        }
    });
}